/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.guobo.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.guobo.entity.ExhHall;
import com.thinkgem.jeesite.modules.guobo.entity.Location;
import com.thinkgem.jeesite.modules.guobo.service.ExhHallService;
import com.thinkgem.jeesite.modules.guobo.service.LocationService;

/**
 * 位置Controller
 * @author wudavid
 * @version 2017-03-09
 */
@Controller
@RequestMapping(value = "${adminPath}/guobo/location")
public class LocationController extends BaseController {

	@Autowired
	private LocationService locationService;
	
	@Autowired
	private ExhHallService exhHallService;
	
	@ModelAttribute
	public Location get(@RequestParam(required=false) String id) {
		Location entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = locationService.get(id);
		}
		if (entity == null){
			entity = new Location();
		}
		return entity;
	}
	
	@RequiresPermissions("guobo:location:view")
	@RequestMapping(value = {"list", ""})
	public String list(Location location, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Location> page = locationService.findPage(new Page<Location>(request, response), location); 
		model.addAttribute("page", page);
		return "modules/guobo/locationList";
	}

	@RequiresPermissions("guobo:location:view")
	@RequestMapping(value = "form")
	public String form(Location location, Model model) {
		model.addAttribute("location", location);
		return "modules/guobo/locationForm";
	}

	@RequiresPermissions("guobo:location:edit")
	@RequestMapping(value = "save")
	public String save(Location location, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, location)){
			return form(location, model);
		}
		locationService.save(location);
		addMessage(redirectAttributes, "保存位置成功");
		return "redirect:"+Global.getAdminPath()+"/guobo/location/?repage";
	}
	
	@RequiresPermissions("guobo:location:edit")
	@RequestMapping(value = "delete")
	public String delete(Location location, RedirectAttributes redirectAttributes) {
		locationService.delete(location);
		addMessage(redirectAttributes, "删除位置成功");
		return "redirect:"+Global.getAdminPath()+"/guobo/location/?repage";
	}

	/**
	 * 获取展馆JSON数据
	 * @param response
	 * @return
	 */
	@RequiresPermissions("guobo:location:edit")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<ExhHall> list = exhHallService.findList(new ExhHall());
		for (int i=0; i<list.size(); i++){
			ExhHall e = list.get(i);
			Map<String, Object> map = Maps.newHashMap();
			map.put("id", e.getId());
			map.put("pId", "0");
			map.put("pIds", "0,");
			map.put("name", e.getName());
			map.put("isParent", true);
			mapList.add(map);
			List<Location> locations = locationService.findListByExhHallId(e.getId());
			for (Location location : locations) {
				Map<String, Object> loc = Maps.newHashMap();
				loc.put("id", location.getId());
				loc.put("pId", e.getId());
				loc.put("pIds", e.getId());
				loc.put("name", location.getName());
				mapList.add(loc);
			}
		}
		return mapList;
	}
}